perf: mirror ASOF equality-key filters to right - #25574
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #25574 +/- ##
==========================================
- Coverage 82.48% 82.48% -0.01%
==========================================
Files 1140 1140
Lines 438540 438520 -20
Branches 438540 438520 -20
==========================================
- Hits 361751 361722 -29
- Misses 54946 54954 +8
- Partials 21843 21844 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…r-mirroring # Conflicts: # benchmarks/sql_benchmarks/asof_join/asof_join.suite # datafusion/optimizer/src/push_down_filter.rs # datafusion/sqllogictest/test_files/asof_join.slt
|
Hi @jayzhan211 and @2010YOUY01, this follow-up is now restacked and ready for review. Would you mind taking a look when you have time? Thanks! |
|
Thanks @Xuanwo , one suggestion here The hand-written - // A literal comparison on an equal, same-typed key has the
- // same value for every matching pair. Mirroring it to the
- // right can prune groups without changing the ASOF candidate.
- let mut right_predicates = Vec::new();
- for predicate in &push_predicates {
- ...
- }
+ // Every matching pair has equal key values, so a deterministic
+ // predicate over left keys holds for the matching right keys.
+ let join_col_keys = join
+ .on
+ .iter()
+ .filter_map(|(l, r)| Some((l.try_as_col()?, r.try_as_col()?)))
+ .collect::<Vec<_>>();
+ let mut inferred = InferredPredicates::new(JoinType::Inner);
+ infer_join_predicates_impl::<true, false>(
+ &join_col_keys,
+ &push_predicates,
+ &mut inferred,
+ )?;
+ let right_predicates = inferred
+ .predicates
+ .into_iter()
+ .filter(|p| {
+ p.column_refs()
+ .iter()
+ .all(|c| join.right.schema().is_column_from_schema(c))
+ })
+ .collect::<Vec<_>>();With this change, both of these prune the right input as well: SELECT * FROM l ASOF JOIN r MATCH_CONDITION (l.ts >= r.ts) ON l.k = r.k WHERE l.k IN (1, 3);
SELECT * FROM l ASOF JOIN r MATCH_CONDITION (l.ts >= r.ts) ON l.k = r.k WHERE l.k > 1; |
2010YOUY01
left a comment
There was a problem hiding this comment.
Thank you. It's a good idea, and the change looks safe.
I think this util is a neat idea, but a bit hard to wrap my head around and get convinced it's safe to reuse; However we can implement something similar for ASOF joins. |
nit: We could scale this benchmark query up so a run takes around 1s. At ~2ms per run, the numbers are easily swayed by noise, e.g. a false-positive 2x slowdown. |
|
Thanks @jayzhan211 and @2010YOUY01. I kept this PR focused and opened #25728 with an ASOF-specific key mapping. I also scaled Q09 to about 1s on the baseline and reran it. The new median is 972.1ms vs 78.5ms across 14 runs. |
|
Thanks @Xuanwo and @2010YOUY01 ! |
Which issue does this PR close?
Rationale for this change
When a filter selects one ASOF equality-key group on the left, rows in other right-side groups cannot match any surviving left row. The broadcast join still sorts and retains those right rows unless the same key filter is applied to the right input.
Performance
Q09 has 50M rows on each side across 500K equality groups.
WHERE l.key = 42retains one group and produces 100 rows with both revisions. On an Apple M4 Max, using independent release builds and two 7-iteration runs per revision, the median elapsed time across all 14 iterations was:mainwithout this optimization (92559afc0)c7522fa37)This selective keyed workload is about 12.4x faster (91.9% lower elapsed time). The benchmark file and command were the same for both binaries:
What changes are included in this PR?
left_key = literalpredicate to the corresponding right key when both join keys are direct columns of the same type.asof_join.slt, plus a keyed selective workload as ASOF benchmark Q09.What is the testing strategy for this PR?
The SLT cases check the mirrored plan, matching and unmatched results, a right-side
IS NULLfilter, and a left disjunction that must not prune the right input. Q09 runs the user-visible query with 50M rows per side across 500K groups, selecting one group.Are there any user-facing changes?
No API or result changes. Eligible ASOF joins can sort and retain fewer right-side rows.